package com.example.team.dao.Imp;

import com.example.team.dao.IMenuDao;
import com.example.team.entity.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *作者：代志雄
 * 2023年11月3日11点35分
 */
@Repository
public class IMenuDaoImpl implements IMenuDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertMenu(Menu menu) {
        return jdbcTemplate.update(
                "insert into menu(menuId,MenuName,MenuURL,JobId) " +
                        "values(?,?,?,?)"
                ,menu.getMenuId()
                ,menu.getMenuName()
                ,menu.getMenuURl()
                ,menu.getJobId()
        );
    }

    @Override
    public int deleteMenu(Menu menu) {
        return jdbcTemplate.update(
                "delete from menu " +
                "where MenuID=?",menu.getMenuId());

    }

    @Override
    public int updateMenu(Menu menu) {
        return jdbcTemplate.update("update menu set " +
                "MenuID=?,MenuName=?,MenuURL=?,JobID=?"
                ,menu.getMenuId()
                ,menu.getMenuName()
                ,menu.getMenuURl()
                ,menu.getJobId()
        );
    }

    @Override
    public List<Menu> getAllMenu(Menu menu) {
        if(menu.getMenuName()!=null)
            return jdbcTemplate.query("select " +
                "menuId,MenuName,MenuURL,JobId" +
                "from menu where MenuName='"+menu.getMenuName()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(Menu.class));
        else
            return jdbcTemplate.query("select " +
                "menuId,MenuName,MenuURL,JobId " +
                "from menu",new Object[]{},
                new BeanPropertyRowMapper<>(Menu.class));
    }

    @Override
    public Menu getMenu(Menu menu) {
        List<Menu> result=jdbcTemplate.query("select " +
                                "menuId,MenuName,MenuURL,JobId " +
                                "from menu where MenuName='"+menu.getMenuName()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(Menu.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
